//
//  HYVideoMoreListView.m
//  XPlayer
//
//  Created by 张水 on 2023/10/12.
//

#import "HYVideoMoreListView.h"
#import "HYVideoMoreListCell.h"


#define kDefaultRow 5

@interface HYVideoMoreListView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSArray *videoLists;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) CGFloat cellHeight;

@end

@implementation HYVideoMoreListView

- (instancetype)initWithCurrentVideoModel:(HYVideoModel *)currentModel
                               videoLists:(NSArray *)videoLists
{
    self = [super init];
    if(self){
        _currentModel = currentModel;
        _videoLists = videoLists;
        _cellHeight = currentModel.isAudio?54:64;
        [self configUI];
    }
    return self;
}


- (void)configUI
{
    self.backgroundColor = AppContentBgThemeColor;
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.right.equalTo(self);
        make.bottom.mas_equalTo(-15);
    }];
    if(self.videoLists.count > kDefaultRow){
        @weakify(self);
        NSInteger row = [self.videoLists indexOfObject:self.currentModel];
        [[[[[[[RACObserve(self.tableView, contentSize) distinctUntilChanged] filter:^BOOL(id value) {
                                    return [value CGSizeValue].height>0;
                }] take:1] delay:0.2]deliverOn:[RACScheduler mainThreadScheduler]]takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
                    @strongify(self);
                    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
                }];
    }
}


#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoLists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYVideoMoreListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HYVideoMoreListCell" forIndexPath:indexPath];
    [cell bindViewModel:self.videoLists[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    HYVideoModel *model = self.videoLists[indexPath.row];
    if(self.currentModel != model){
        self.currentModel.isSelect = NO;
        model.isSelect = YES;
        if([self.delegate respondsToSelector:@selector(changeVideoWithIndexPath:)]){
            [self.delegate changeVideoWithIndexPath:indexPath.row];
        }
    }
}

- (void)setCurrentModel:(HYVideoModel *)currentModel
{
    _currentModel = currentModel;
    [self.tableView reloadData];
}

- (void)showView:(UIView *)superView withViewPopupMode:(View_Popup_Mode)mode
{
    CGFloat height = kDefaultRow*self.cellHeight+30;
    CGFloat width = 350.f;
    if (mode == View_Popup_Mode_Down){
        width = kMainScreenWidth;
    }
    if (mode == View_Popup_Mode_Right) {
        height = kMainScreenWidth;
    }
    self.frame = CGRectMake(0, 0, width, height);
    @weakify(self);
    [HYAlertShowAnimate showInView:superView
                         alertView:self
                         popupMode:mode
                           bgAlpha:0.3
                  outsideDisappear:YES disappear:^{
                @strongify(self);
                if([self.delegate respondsToSelector:@selector(dismiss)]){
                    [self.delegate dismiss];
                }
    }];
}

#pragma mark - loadLazy
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[HYVideoMoreListCell class] forCellReuseIdentifier:@"HYVideoMoreListCell"];
    }
    return _tableView;
}


@end
